Repository navigation
fix(init): recover a lockless index root instead of dead-ending - #204
Merged
Merged
Conversation
A failed background daemon start could leave `.codegraph/` holding nothing but `daemon.log`, because the parent created the index root solely to place the child's stdio. That shape is unrecoverable: `init`, `index --force`, `sync` and `uninit --force` all refuse it, and the log itself keeps advising `codegraph init` — the very command the daemon had just made impossible. Only `rm -rf .codegraph` got out. Teach explicit init to take over such a root. The recovery predicate is now bare `ExtractionStatus::Missing`, which holds only when neither state slot exists: any unreadable slot classifies `Corrupt` earlier, so widening the predicate cannot reach authoritative data. Creating the permanent lock is not possessing it. Between `create_new` and the first `try_lock` the file exists unlocked, so a competing writer can acquire it and publish state; `final_path_matches` corroborates inode identity, not namespace state. Re-assert `Missing` while holding the lease and refuse with `LocklessTakeoverStateChanged` otherwise, dropping the lease before any authorization, `phase=building` publication or database mutation. A `test-hooks` checkpoint parks a real `init` child in that pre-lock window so the race is covered deterministically, without sleeps. Stop the spawn from creating an index root just to hold diagnostics: the log redirect now falls back to the null sink when the root is absent, and the daemon refusal no longer claims no log was published when the parent wrote one. `LockNotFound` and lockless `Missing` now name a remedy that works instead of an internal artifact.
Codecov Report❌ Patch coverage is
@@ Coverage Diff @@
## main #204 +/- ##
==========================================
+ Coverage 95.16% 95.18% +0.02%
==========================================
Files 136 136
Lines 72320 73024 +704
==========================================
+ Hits 68822 69510 +688
- Misses 3498 3514 +16
... and 4 files with indirect coverage changes 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The dead end
A user's
codegraph initfailed permanently on a project that had never been indexed:.codegraph/held exactly one file —daemon.log— with four identical entries over two days saying the daemon refused to start because index state was missing, and advisingcodegraph init. The daemon had already made that impossible.Measured on the reproduction:
init,index --force,sync, anduninit --forceall exit 1. Onlystatusruns, reportingState: missingwith no remedy. The single way out wasrm -rf .codegraph.The shape is trivial to create: the parent process called
create_dir_allon the index root purely to place the detached child's stdio, so a failed background start left a directory containing a log and nothing else.Fixes
A —
inittakes over a lockless, state-less root. The recovery predicate is now bareExtractionStatus::Missing, which holds only when neither state slot exists: any unreadable slot classifiesCorruptearlier, so widening the predicate cannot reach authoritative data. Verified: a lockless root with a malformedindex-state.0.jsonstill exits 1, keeps the slot, and creates no lock.Creating the lock is not possessing it. Between
create_newand the firsttry_lockthe file exists unlocked, so a competing writer can acquire it and publish state;final_path_matchescorroborates inode identity, not namespace state. So the recovery arm re-assertsMissingwhile holding the lease and refuses withLocklessTakeoverStateChangedotherwise, dropping the lease before any authorization,phase=buildingpublication, or database mutation.B — the spawn no longer creates an index root to hold diagnostics. The log redirect falls back to the null sink when the root is absent, and the daemon refusal stops claiming no log was published when the parent had written one.
C — diagnostics name a remedy that works.
LockNotFoundand locklessMissingnow point atcodegraph statusand say plainly that onlyState: missingis safe to recover withcodegraph init, instead of naming an internal artifact.Why the race test is trustworthy
A
test-hooks-only checkpoint parks a realinitchild in the pre-lock window. The test then acquires the still-unlocked permanent lock itself, publishesBuilding, and releases the child — proving the window exists rather than asserting it. No sleeps; the rendezvous is a TCP handshake and the timeouts are hang-guards only.Reverting just the re-assertion makes that test fail because the takeover succeeds —
Indexed 3 files— while the competitor'sBuildingstate is live. That is the hazard: destroying another process's in-progress work. The test was also run 20× consecutively without a flake.Verification
make cigreen: 127 suites / 3156 passed / 0 failed (baseline 3146).initnow succeeds, the sentineldaemon.logis byte-identical afterwards, andindex.lock+codegraph.dbappear.--forcestate override, no widened lock creation forBuilding/Corrupt/Future, no takeover of a namespace with a valid slot, and the globalFullRebuildgate is unchanged.reference/untouched,CURRENT_EXTRACTION_VERSIONstill 5, guardrail passes.Third instance of one family:
v0.42.5taughtinitto take over a legacy.codegraphclassifyingMissingwith a database present,v0.42.9when every slot wasOwnerMismatchafter a project move. Each time a stateinitcould not recover from, plus an error naming an internal artifact instead of a remedy.